/**
 * Created by jackliu on 15/12/16.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var VideoDesignDoc = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  /**
   *`design_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '设计模版id',
   *`design_name` varchar(100) DEFAULT NULL COMMENT '设计模版名',
   *`item_id` bigint(20) unsigned DEFAULT NULL COMMENT '关联的商品id',
   *`user_id` bigint(20) NOT NULL COMMENT '用户id',
   *`user_type` int(1) NOT NULL DEFAULT '0' COMMENT '0后台',
   *`state` int(2) NOT NULL DEFAULT '10' COMMENT '0未知;10制作中;20提交等待审核;31一级审核通过,32二级审核通过;41一级审核失败,42二级审核失败',
   *`publish` int(1) NOT NULL DEFAULT '0' COMMENT '0未发布; 1已发布',
   *`vip` int(2) NOT NULL DEFAULT '0' COMMENT '0普通模版, 10vip专属模版',
   *`tg_data` mediumtext COMMENT '提纲数据',
   *`v_data` mediumtext COMMENT '视频文档数据',
   *`hash_tag` varchar(100) DEFAULT '' COMMENT '标签',
   *`thumb` text COMMENT '缩略图',
   *`video` text COMMENT '视频地址',
   *`duration` float NOT NULL DEFAULT '0' COMMENT '视频时长',
   *`size_id` bigint(20) NOT NULL COMMENT '尺寸',
   *`type_id` bigint(20) NOT NULL COMMENT '分类',
   *`price` bigint(20) NOT NULL DEFAULT '0' COMMENT '设计费用(单位:rmb分)',
   *`order` int(3) NOT NULL DEFAULT '999' COMMENT '显示顺序',
   *`ver` int(10) NOT NULL DEFAULT '1' COMMENT '版本号',
   *`create_time` datetime NOT NULL,
   *`update_time` datetime NOT NULL,
   *`update_by` varchar(50) NOT NULL DEFAULT '',
   *`del_flg` int(1) NOT NULL DEFAULT '0',
   *PRIMARY KEY (`design_id`),
   *
   *注意:
   *     视频设计模版表
   *     1. 修改［未发布］模版a时，直接update
   *     2. 修改［已发布］模版a时，先将a取消发布，再将a拷贝成b，再将a逻辑删除，后续修改在b模版上进行
   *     3. 发布模版a时，需要将商品表design_id更新为a的id
   *
   * */
};
VideoDesignDoc.prototype = new ItbBaseModel();

// 取得表名
VideoDesignDoc.prototype.getTableName = function(){
    return 'video_design_doc';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
VideoDesignDoc.prototype.getWhereConditionOfKeyEqual = function(){
    return 'design_id='+this.design_id;
};
//------------------------------------------------------------------

/**
 * 获取模板提纲信息(包含尺寸信息)
 *
 * @param designId 模板唯一标识
 * @param callback 回调
 *              err: 错误原因
 *              data: 模板行数据 + 宽+高+单位
 * */
VideoDesignDoc.prototype.getDesignDocInfo = function(designId, callback){

    pool.pool.getConnection(function(getConErr, connection) {
        if (getConErr) {
            console.log('VideoDesignDoc.getDesignDocInfo Err:'+getConErr);
            return callback(getConErr, null);
        }

        var sql =
            ' SELECT vd.*, ' +
            '        ts.width, ' +
            '        ts.height, ' +
            '        ts.unit ' +
            ' FROM video_design_doc vd, ' +
            '      tg_size ts ' +
            ' WHERE vd.design_id = ' + designId +
            ' AND vd.publish = 1 '  + // 已发布
            ' AND vd.state = 32  ' +  // 二级审核通过
            ' AND vd.del_flg=0 ' +
            ' AND ts.size_id = vd.size_id ' +
            ' AND ts.del_flg=0 ' ;

        console.log(sql);
        connection.query(sql, function (error, result) {
            connection.release();

            if(result && result.length>0) {
                return callback(error, result[0]);
            } else {
                return callback(error, null);
            }
        });
    });
};

//------------------------------------------------------------------
/* 一级审核通过
 * 参数:user_id: 用户id
 * 参数:design_id: 设计模版id
 * 参数:callback:回调
 * */
VideoDesignDoc.prototype.approveDesign1 = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = 'UPDATE video_design_doc '+
            ' SET state=31, update_time=sysdate(), update_by= '+THIS.update_by+
            ' WHERE del_flg = 0 AND state=20 AND design_id='+ THIS.design_id;

        //var sqlValue = [THIS.update_by, THIS.doc_id];
        //var sql = mysql.format(sql, sqlValue);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            if(error) {
                callback(error);
            } else {
                callback(null);
            }
        });
    });
};
//------------------------------------------------------------------
/* 一级审核失败
 * 参数:user_id: 用户id
 * 参数:design_id: 设计模版id
 * 参数:callback:回调
 * */
VideoDesignDoc.prototype.disapproveDesign1 = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = 'UPDATE video_design_doc '+
            ' SET state=41, update_time=sysdate(), update_by= '+THIS.update_by+
            ' WHERE del_flg = 0 AND state=20 AND design_id='+ THIS.design_id;

        //var sqlValue = [THIS.update_by, THIS.doc_id];
        //var sql = mysql.format(sql, sqlValue);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            if(error) {
                callback(error);
            } else {
                callback(null);
            }
        });
    });
};


/**
 * 获取模板提纲信息(包含尺寸信息), debug
 *
 * @author 王俊
 * @date 2017-12-27
 *
 * @param designId 模板唯一标识
 * @param callback 回调
 *              err: 错误原因
 *              data: 模板行数据 + 宽+高+单位
 * */
VideoDesignDoc.prototype.getDesignDocInfo_for_debug = function(designId, callback){

    pool.pool.getConnection(function(getConErr, connection) {
        if (getConErr) {
            console.log('VideoDesignDoc.getDesignDocInfo Err:'+getConErr);
            return callback(getConErr, null);
        }

        var sql =
            ' SELECT vd.*, ' +
            '        ts.width, ' +
            '        ts.height, ' +
            '        ts.unit ' +
            ' FROM video_design_doc vd, ' +
            '      tg_size ts ' +
            ' WHERE vd.design_id = ' + designId +
            // ' AND vd.publish = 1 '  + // 已发布
            // ' AND vd.state = 32  ' +  // 二级审核通过
            ' AND vd.del_flg=0 ' +
            ' AND ts.size_id = vd.size_id ' +
            ' AND ts.del_flg=0 ' ;

        console.log(sql);
        connection.query(sql, function (error, result) {
            connection.release();

            if(result && result.length>0) {
                return callback(error, result[0]);
            } else {
                return callback(error, null);
            }
        });
    });
};

// 导出
module.exports = VideoDesignDoc;